var roleManage = function() {
	var _init = function() {
		var  table = $('#table_role');
		table.dataTable({
			"processing" : true,
			serverSide : true,// 开启服务器模式
			"ajax" : {
				url : list_url,  // 列表查询
				data:{}
			},
			"autoWidth" : true,
			"columns" : [
				{
					"width" : "20%",
					"data" : "name",
					"render" : function(data, type, row, meta) {
						return data;
					}
				},		
				{
					"width" : "20%",
					"data" : "code",
					"render" : function(data, type, row, meta) {
						return data;
					}
				},
				{
					"width" : "20%",
					"data" : "secretKey",
					"render" : function(data, type, row, meta) {
						return data;
					}
				},
				{
					"width" : "20%",
					"data" : "store.name",
					"render" : function(data, type, row, meta) {
						return data;
					}
				},
				{
					"width" : "10%",
					 "data": "status",
					"render" : function(data, type, row, meta) {
						if(data == 10){
							return '开启';
						}else {
                            return '关闭';
						}

					}
				},
				{
					"width" : "10%",
					"render" : function(data, type, row, meta) {
						return "<button type=\"button\" class=\"btn btn-success btn-xs btn_edit_coupon\">设置</button>&nbsp;"+
						"<button type=\"button\" class=\"btn btn-danger btn-xs btn_del_role\">删除</button>";
					}
				}
			],
            "language": {
                url: '/public/static/assets/pages/Chinese.json'
            },
            "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
            "pageLength": 10,
            "scrollX":"",
            "destroy": false,
            "stateSave": true,
			"dom" : "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>"
					+ "<t>" + "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
		});

		// 删除
		table.on('click', '.btn_del_role', function(event) {
			var oData_arr = table.DataTable().rows($(this).parents("tr")).data(); // 操作行对象
			var _id = oData_arr[0].id;
			bootbox.dialog({
				message : "确定要删除?",
				title : "提示",
				buttons : {
					cancel : {
						label : "取消",
						className : "btn dark btn-outline"
					},
					success : {
						label : "确定",
						className : "btn green",
						callback : function() {
							$.ajax({
                                url : del_url,
                                type : 'delete',
                                dataType : 'json',
                                contentType:"application/json; charset=utf-8",
                                data : JSON.stringify({ id:_id}),
                                async: false
                            }).done(function(data) {
                                if (data.status == '1') {
                                    layer.msg(data.message, {icon: 6, time: 3000});
                                    //table.DataTable().ajax.reload();
                                    table.DataTable().draw(false);
                                } else if (data.status == '0') {
                                    layer.msg(data.message, {icon: 5, time: 3000});
                                }
                            });
						}
					}
				}
			});
		});

		//编辑
		table.on('click', '.btn_edit_coupon', function(event) {
			var oData_arr = table.DataTable().rows($(this).parents("tr")).data(); // 操作行对象
	        var _id = oData_arr[0].id;
	        var _storeId = oData_arr[0].storeId;
	        window.location.href = edit_url + "?id="+ encodeURIComponent(_id);
		});

			
		//添加新代金券
		$("#add_printer").on("click",function(){
			window.location.href = edit_url;
		});
		
		
	}

	return {
		init : function() {
			_init();
		}
	};

}();

$(function() {
	roleManage.init();

    if ($("input[name='printerMethod']:checked").val() == "3") {
        $(".sort").show();
    }else{
        $(".sort").hide();
    }
	$("input[name='printerMethod']").change(function(){
		if ($("input[name='printerMethod']:checked").val() == "3") {
			$(".sort").show();
		}else{
			$(".sort").hide();
		}
	})
	$('[name="switch"]').bootstrapSwitch();
	/*$('[name="switch"]').bootstrapSwitch({  
        onSwitchChange:function(event,state){  
            if(state==true){  
            	$('[name="switch"]').val("1");  
            }else{  
            	$('[name="switch"]').val("0");  
            }  
        }  
    })*/
});


function beback(){
	window.location.href = index_url;
}

//var storeId = $('#modal_new_role').find("input[name='storeId']").val();
// 添加/编辑保存
$('#save').click(function() {
	var _data = $("#modal_new_role").serializeObject();
	if (!mdTool.validate(_data.name, 'require')) {
		mdTool.showMessage('小票机名称');
		return;
	}
	if (!mdTool.validate(_data.code, 'require')) {
		mdTool.showMessage('小票机编号');
		return;
	}
    if (!mdTool.validate(_data.secretKey, 'require')) {
        mdTool.showMessage('小票机密钥');
        return;
    }
    var switch1 = $("#switch1").is(":checked");
    var switch2 = $("#switch2").is(":checked");
    var switch3 = $("#switch3").is(":checked");
    var switch4 = $("#switch4").is(":checked");
    var status = $("#isopen").is(":checked");
    var peisong_status = $("#peisong_isopen").is(":checked");
    var isauto = $("#isauto").is(":checked");

    if(switch1){
    	_data.duizhang = 'on';
	}
	if(switch2){
        _data.chuping = 'on';
        if(_data.printerMethod == 3){
        	if(!mdTool.validate(_data.category, 'require')){

                layer.msg('请选择分类打印类目', {icon: 5, time: 3000});
                return;
			}
		}
	}
    if(switch3){
        _data.jiezhang = 'on';
    }
    if(switch4){
        _data.isauto = 'on';
    }
    if(status){
        _data.status = 10;
	}else {
        _data.status = 0;
	}
	if(peisong_status){
        _data.peisong_status = 1;
	}else {
        _data.peisong_status = 0;
	}
	if(isauto){
        _data.isauto = 1;
	}else {
        _data.isauto = 0;
	}
    if(!switch1 && !switch2 && !switch3){
        layer.msg('必须选择一种类型的小票', {icon: 5, time: 3000});
        return;
	}
	console.log(_data);
	
	mdTool.showMessage('');
	//数据提交
	$.ajax({
        url :savePost_url,
        type : 'POST',
        dataType : 'json',
        contentType:"application/json; charset=utf-8",
        data : JSON.stringify(_data),
    }).done(function(data) {
    	console.log(data);
        if(data.status == '0') {
            layer.msg(data.message, {icon: 5, time: 3000});
        } else if(data.status == '1'){
            layer.msg('保存成功', {icon: 6, time: 3000});
            location.href = index_url;
        }
    });



    // $("#modal_new_role").submit();
});



	



